from pages.base_page import BasePage
from selenium.webdriver.common.by import By
from time import sleep
import re


class StockInPage(BasePage):
    url = '/manage/stock/stock-in'
    upload_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div/div[2]/div/div[1]/div/div[2]/div[1]/label')
    submit_upload_locater = (By.XPATH,'/html/body/div[3]/div/div[2]/div/div[2]/div[3]/button[2]')
    wait_in_stock_tab_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div/div[2]/div/div[1]/div/div[1]/div/div/div/label[1]/div')
    stock_in_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div/div[2]/div/div[2]/div/div/div/div/div[2]/table/tbody/tr[2]/td[9]/div/button[1]/span')
    first_stock_uniquecode_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div/div[2]/div/div[2]/div/div/div/div/div[2]/table/tbody/tr[2]/td[1]/span')
    shelf_select_locater = (By.XPATH,'/html/body/div[3]/div/div[2]/div/div[2]/div[2]/form/div/div/div/div/div/div')
    first_shelf_locater = (By.XPATH,'/html/body/div[3]/div/div[2]/div/div[2]/div[2]/form/div/div/div/div/div/div/div/span[2]')
    confirm_shelf_locater = (By.XPATH, '/html/body/div[3]/div/div[2]/div/div[2]/div[3]/div/div/button[2]/span')
    stock_out_module_locater = (By.XPATH,'//*[@id="rc-menu-uuid-05211-3-/manage/stock-popup"]/li[2]/span/a')
    first_stock_out_uniquecode_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div[1]/div[2]/div/div[2]/div/div/div/div/div[2]/table/tbody/tr[2]/td[1]/span')
    unicode_search_locater = {By.XPATH,'//*[@id="uniqueCode"]'}
    search_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div/div[1]/form/div/div[6]/div/div/div[2]/div/div/div/div[1]/div/button[2]')
    first_cancel_stock_in_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div/div[2]/div/div[2]/div/div/div/div/div[2]/table/tbody/tr[2]/td[9]/div/button[2]/span')
    confirm_cancel_locater = (By.XPATH,'/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[2]/button[2]/span')
    canceled_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div/div[2]/div/div[1]/div/div[1]/div/div/div/label[2]/div')
    second_stock_unicode_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div/div[2]/div/div[2]/div/div/div/div/div[2]/table/tbody/tr[3]/td[1]/span')
    batch_stock_in_button_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div/div[2]/div/div[1]/div/div[2]/div[1]/button/span')
    batch_unicode_search_locater = (By.XPATH,'/html/body/div[3]/div/div[2]/div/div[2]/div[2]/form/div[1]/div/div[2]/div/div/span/span/span[1]/input')
    batch_shelf_locater = (By.XPATH,'//*[@id="shelfId"]')
    batch_confirm_locater = (By.XPATH,'/html/body/div[3]/div/div[2]/div/div[2]/div[3]/div/div/button[2]/span')




    def click_upload_file(self):
        """点击导入库存"""
        self.click(self.upload_locater)

    def click_submit_upload(self):
        """确认上传"""
        self.click(self.submit_upload_locater)

    def get_wait_in_stock_num(self):
        """获取待入库数量"""
        text = self.get_text(self.wait_in_stock_tab_locater)
        numbers = re.findall(r'\d+', text)
        return int(numbers[0])


    def get_first_stock_in_uniquecode(self):
        """获取第一个待入库库存唯一码"""
        return self.get_text(self.first_stock_uniquecode_locater)

    def get_second_stock_in_uniquecode(self):
        """获取第二个待入库库存唯一码"""
        return self.get_text(self.second_stock_unicode_locater)

    def click_first_in_stock(self):
        """点击第一个待入库库存的确认按钮"""
        self.click(self.stock_in_locater)

    def select_first_shelf(self):
        """单个入库弹窗选择第二个货架"""
        self.click(self.shelf_select_locater)
        self.click_position(801,628)

    def confirm_shelf_select(self):
        """货架弹窗点击确认"""
        self.click(self.confirm_shelf_locater)

    def switch_stock_out(self):
        """切换至出库管理"""
        self.click_position(87,273)


    def search_unicode(self, unicode):
        """通过唯一码搜索"""
        self.send_keys(self.unicode_search_locater,unicode)
        self.click(self.search_locater)

    def cancel_first_stock(self):
        """取消第一个库存入库"""
        self.click(self.first_cancel_stock_in_locater)
        self.click(self.confirm_cancel_locater)

    def click_canceled(self):
        """点击入库管理已取消tab"""
        self.click(self.canceled_locater)

    def click_batch_stock_in(self):
        self.click(self.batch_stock_in_button_locater)

    def batch_search_unicode(self,unicode):
        """批量入库弹窗搜索唯一码"""
        self.send_keys(self.batch_unicode_search_locater,unicode)
        # self.click_position(646,272)
        # self.sendkeys_position(unicode)
        sleep(2)

    def batch_choose_first_shelf(self):
        """批量入库弹窗选择第一个货架"""
        self.click(self.batch_shelf_locater)
        self.click_position(623,472)

    def confirm_batch_stock_in(self):
        """确认批量入库"""
        self.click(self.batch_confirm_locater)
